package tw.cating.karmasaver.plurk.auth;

import tw.cating.karmasaver.Constants;
import tw.cating.karmasaver.intef.finishListener;
import tw.cating.karmasaver.plurk.plurkApi;
import android.accounts.AbstractAccountAuthenticator;
import android.accounts.Account;
import android.accounts.AccountAuthenticatorResponse;
import android.accounts.AccountManager;
import android.accounts.NetworkErrorException;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class plurkAuth extends AbstractAccountAuthenticator{
	Context mContext = null;
	plurkApi plurk = null;
	
	public plurkAuth(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		mContext = context;
		plurk = new plurkApi(mContext);
	}

	@Override
	public Bundle addAccount(AccountAuthenticatorResponse response,
			String accountType, String authTokenType,
			String[] requiredFeatures, Bundle options)
			throws NetworkErrorException {
		// TODO Auto-generated method stub
		final Intent intent = new Intent(mContext, authActivity.class);
		intent.putExtra(authActivity.PARAM_AUTHTOKEN_TYPE, authTokenType);
		intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE,
				response);
		AccountManager am = AccountManager.get(mContext);
		Account[] accs = am.getAccountsByType(Constants.ACCOUNT_TYPE);
		if (accs.length > 0) {
			Account acc = accs[0];
			intent.putExtra(authActivity.PARAM_USERNAME, acc.name);
			intent.putExtra(authActivity.PARAM_PASSWORD, am.getPassword(acc));
		}
		final Bundle bundle = new Bundle();
		bundle.putParcelable(AccountManager.KEY_INTENT, intent);
		return bundle;
	}

	@Override
	public Bundle confirmCredentials(AccountAuthenticatorResponse response,
			Account account, Bundle options) throws NetworkErrorException {
		// TODO Auto-generated method stub
		Log.i("Cating", "confirmCredentials");
		return null;
	}

	@Override
	public Bundle editProperties(AccountAuthenticatorResponse response,
			String accountType) {
		// TODO Auto-generated method stub
		return null;
	}

	boolean verified = false;
	boolean bWaitThreadFinish = false;
	@Override
	public Bundle getAuthToken(AccountAuthenticatorResponse response,
			Account account, String authTokenType, Bundle options)
			throws NetworkErrorException {
		// TODO Auto-generated method stub
        if (!authTokenType.equals(Constants.AUTHTOKEN_TYPE)) {
            final Bundle result = new Bundle();
            result.putString(AccountManager.KEY_ERROR_MESSAGE,
                "invalid authTokenType");
            return result;
        }
        
        final AccountManager am = AccountManager.get(mContext);
        final String password = am.getPassword(account);
        verified = false;
        if (password != null) {
        	bWaitThreadFinish = true;
            plurk.requestLogin(account.name, password, new finishListener() {

					public void onFinish(int result) {
						// TODO Auto-generated method stub
						if (result == plurkApi.LOGINRESULT_SUCCESSED)
							verified = true;
						else 
							verified = false;
						bWaitThreadFinish = false;
					}
                });
            while (bWaitThreadFinish);
            
            if (verified) {
                final Bundle result = new Bundle();
                result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
                result.putString(AccountManager.KEY_ACCOUNT_TYPE,
                    Constants.ACCOUNT_TYPE);
                result.putString(AccountManager.KEY_AUTHTOKEN, password);
                return result;
            }
        }
		return null;
	}

	@Override
	public String getAuthTokenLabel(String authTokenType) {
		// TODO Auto-generated method stub
		Log.i("Cating", "getAuthTokenLabel");
		return null;
	}

	@Override
	public Bundle hasFeatures(AccountAuthenticatorResponse response,
			Account account, String[] features) throws NetworkErrorException {
		// TODO Auto-generated method stub
		Log.i("Cating", "hasFeatures");
		return null;
	}

	@Override
	public Bundle updateCredentials(AccountAuthenticatorResponse response,
			Account account, String authTokenType, Bundle options)
			throws NetworkErrorException {
		// TODO Auto-generated method stub
		Log.i("Cating", "updateCredentials");
		return null;
	}

}
